
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author BENSON
 */
public class ControllerAddOrder {
     Connection conn=null;
    String deleteStudent;
    String querry;
    Statement st=null;
   public EnterForm EnterOrder;
    
    private ActionListener actionListener;
    
    public ControllerAddOrder( ){
       // this.deleteOrder = delete;
        EnterOrder = new EnterForm();
        EnterOrder.setVisible(true);
                          
    }
   
     
    public void addOrders(){
    try{
       Class.forName("com.mysql.jdbc.Driver");
       conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/jetwingdb?zeroDateTimeBehavior=convertToNull","root","");
       
        querry=("INSERT INTO orders SET Name=?,passPortNo=? ,email =?,phoneNo =?,flightNo =?,seatNo=? ,from =?,destination =?,depatureTime =?, arrivalTime=?;");
        
    PreparedStatement p =conn.prepareStatement(querry);
    p.setString(1, EnterOrder.getFName().getText());
    p.setString(2, EnterOrder.pasportNo().getText());
    p.setString(3, EnterOrder.getEmail().getText());
    p.setString(4, EnterOrder.getPhoneNo().getText());
    p.setString(5, EnterOrder.getFlightNo().getText());
    p.setString(6, EnterOrder.getSeatNo().getText());
    p.setString(7, EnterOrder.getFrom().getText());
    p.setString(8, EnterOrder.getDestination().getText());
    p.setString(9, EnterOrder.getDptTime().getText());
    p.setString(10, EnterOrder.getArrivalTime().getText());
    p.execute();
        
    p.close();
   conn.close();
  JOptionPane.showMessageDialog(null, "Order has been succesfully added");
    }  
    catch(Exception e){
    JOptionPane.showMessageDialog(null,e);
    }
    
    
    }
     public void contol(){        
        actionListener = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {  
                  if(actionEvent.getSource()==EnterOrder.getAddButton()){
                        addOrders();
                  }
                  if(actionEvent.getSource()==EnterOrder.getCloseButton()){
                  EnterOrder.setVisible(false);
                  
                  }
              }
              
        };                
        EnterOrder.getAddButton().addActionListener(actionListener);
        EnterOrder.getCloseButton().addActionListener(actionListener);
    }
     public static void main(String[]args){
         ControllerAddOrder c= new ControllerAddOrder();
         c.contol();
     
     }
}
